<?php

namespace app\admin\controller;

use app\admin\exception\AdminException;
use app\admin\service\rbac\Users\Service;
use app\common\entity\ManageUser;
use app\common\entity\Market;
use app\common\entity\MobileRecharge;
use app\common\entity\Mywallet;
use app\common\entity\MywalletLog;
use app\common\entity\Orders;
use app\common\entity\Message;
use app\common\entity\OtcMatch;
use app\common\entity\RealPass;
use app\common\entity\User;
use app\common\entity\VoucherList;
use app\common\entity\UserProduct;
use app\common\entity\WriteLog;
use app\common\shop\ShopGroupModel;
use think\Db;
use think\facade\Session;
use think\Request;

class Index extends Admin
{
    public function xxx(){
        $service = new \app\admin\service\rbac\Users\Service();

        print_r($service->getMenus());die;
    }
    public function index()
    {
        //会员数量
        $user['total'] = User::count();
        $user['no_pass'] = User::where('real_pass',2)->count();
        $user['today'] = User::whereTime('register_time', 'today')->count();
        #今日营业额
        $tody_money = Db::name('shop_order')->where('status','in','1,2,3')->whereTime('paytime','today')->sum('total');
        #总营业额
        $total_money = Db::name('shop_order')->where('status','in','1,2,3')->sum('total');
        #成团数
        $shop_group = ShopGroupModel::where('status',1)->count();
        #团长
        $team = User::where('team_level','>',0)->where('status',1)->count();
        #代理商
        $agent = User::where('agent_level','>',0)->where('status',1)->count();
        #房主
        $house = User::where('house_level','>',0)->where('status',1)->count();
        #区域代理
        $area = User::where('area_level','>',0)->where('status',1)->count();
        #合伙人
        $partner = User::where('partner_level','>',0)->where('status',1)->count();

        $room['minRoomMember'] = User::where('house_level',1)->count();
        $room['midRoomMember'] = User::where('house_level',2)->count();
        $room['maxRoomMember'] = User::where('house_level',3)->count();

        $room['minRoomShoping'] = User::alias('u')
            ->join('shop_order o','o.uid = u.id')
            ->where('o.status','in','2,3')
            ->where('u.house_level',1)
            ->sum('total');
        $room['midRoomShoping'] = User::alias('u')
            ->join('shop_order o','o.uid = u.id')
            ->where('o.status','in','2,3')
            ->where('u.house_level',2)
            ->sum('total');
        $room['maxRoomShoping'] = User::alias('u')
            ->join('shop_order o','o.uid = u.id')
            ->where('o.status','in','2,3')
            ->where('u.house_level',3)
            ->sum('total');

        $room['minRoomProfit'] = User::alias('u')
            ->join('my_wallet_log l','l.user_id = u.id')
            ->where('l.types',15)
            ->where('u.house_level',1)
            ->sum('number');
        $room['midRoomProfit'] = User::alias('u')
            ->join('my_wallet_log l','l.user_id = u.id')
            ->where('l.types',15)
            ->where('u.house_level',2)
            ->sum('number');
        $room['maxRoomProfit'] = User::alias('u')
            ->join('my_wallet_log l','l.user_id = u.id')
            ->where('l.types',15)
            ->where('u.house_level',3)
            ->sum('number');


        $service = new \app\admin\service\rbac\Users\Service();
        return $this->render('index', [
            'user' => $user,
            'tody_money'=>$tody_money,
            'total_money' =>$total_money,
            'shop_group' =>$shop_group,
            'team' =>$team,
            'agent' =>$agent,
            'house' =>$house,
            'area' =>$area,
            'partner'=>$partner,
            'getManageId'=>$service->getManageId(),
            'room'=>$room
        ]);
    }

    //修改密码
    public function updateInfo(Request $request)
    {
        if ($request->isPost()) {
            $validate = $this->validate($request->post(), '\app\admin\validate\ChangePassword');

            if ($validate !== true) {
                throw new AdminException($validate);
            }

            //判断原密码是否相等
            $model = new \app\admin\service\rbac\Users\Service();
            $user = ManageUser::where('id', $model->getManageId())->find();
            $oldPassword = $model->checkPassword($request->post('old_password'), $user);
            if (!$oldPassword) {
                throw new AdminException('原密码错误');
            }

            $user->password = $model->getPassword($request->post('password'), $user->getPasswordSalt());

            if ($user->save() === false) {
                throw new AdminException('修改失败');
            }
            (new WriteLog())->addLog($model->getAdminName(), '修改密码');

            return json(['code' => 0, 'message' => '修改成功', 'toUrl' => url('login/index')]);
        }
        return $this->render('change');
    }

    //获取交易数据
    protected function getOrders()
    {
        $match = Orders::where('status', Orders::STATUS_DEFAULT)->sum('number');
        $pay = Orders::where('status', Orders::STATUS_PAY)->sum('number');
        $confirm = Orders::where('status', Orders::STATUS_CONFIRM)->sum('number');
        $finish = Orders::where('status', Orders::STATUS_FINISH)->sum('number');

        return [
            'match' => $match,
            'pay' => $pay,
            'confirm' => $confirm,
            'finish' => $finish
        ];

    }

    //统计功能 会员等级处理
    protected function getLevel()
    {
        $model = new User();
        $userTable = $model->getTable();
        $sql = <<<SQL
SELECT count(*) as total,`level` FROM {$userTable} GROUP BY `level`
SQL;
        $userLevel = Db::query($sql);
        $data = [];
        foreach ($userLevel as $item) {
            $data[$item['level']] = $item['total'];
        }
        return $data;
    }

    //魔盒统计
    protected function getMagic()
    {
        $running = UserProduct::where('status', UserProduct::STATUS_RUNNING)->count();
        $stop = UserProduct::where('status', UserProduct::STATUS_STOP)->count();

        return [
            'running' => $running,
            'stop' => $stop
        ];
    }

    //退出系统
    public function logout()
    {
        $service = new Service();
        $admin_name = $service->getAdminName();
        $service->logout();
        (new WriteLog())->addLog($admin_name, '退出后台');
        $this->redirect('admin/login');
    }
    public function delConfig(){
        \app\common\entity\Config::delCache();
    }
    public function clear()
    {
        //清除所有session
        Session::destroy();
    }
}